/*
 * Copyright (c) Jack魏 2024 - 2024, All Rights Reserved.
 */

package cn.jackwei.yiyi.controller.sys;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.core.bean.BeanUtil;
import cn.jackwei.yiyi.pojo.bean.SysPage;
import cn.jackwei.yiyi.pojo.constant.CommonConsts;
import cn.jackwei.yiyi.pojo.enums.ResultMsgEnum;
import cn.jackwei.yiyi.pojo.model.SysPageModel;
import cn.jackwei.yiyi.pojo.query.SysPageQuery;
import cn.jackwei.yiyi.pojo.vo.SysPageVo;
import cn.jackwei.yiyi.service.sys.SysPageService;
import cn.jackwei.yiyi.util.Result;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 页面表Controller类
 *
 * @author Jack魏
 * @since 2024-07-21
 */
@Slf4j
@Validated
@SaCheckLogin
@RestController
@RequestMapping("/sys")
@Tag(name = "页面表接口", description = "页面表接口")
public class SysPageController {
    @Autowired
    private SysPageService sysPageService;

    /**
     * 增加、修改、删除页面表
     *
     * @return 操作是否成功
     */
    @Operation(summary = "增加、修改、删除页面表", description = "增加、修改、删除页面表")
    @PostMapping("/editSysPage")
    public Result<Object> editSysPage(@RequestBody SysPageVo vo) {
        SysPage bean = BeanUtil.toBean(vo, SysPage.class);
        // 为空则新增
        if (bean.getId() == null) {
            sysPageService.save(bean);
            return Result.success(ResultMsgEnum.SUCCESS_SAVE.getMsg());
        } else {
            if (CommonConsts.DEL.equals(bean.getIsDel())) {
                sysPageService.removeById(bean);
                return Result.success(ResultMsgEnum.SUCCESS_DELETE.getMsg());
            } else {
                sysPageService.updateById(bean);
                return Result.success(ResultMsgEnum.SUCCESS_UPDATE.getMsg());
            }
        }
    }

    /**
     * 根据条件分页查询页面表
     *
     * @param query 条件
     * @return 查询列表
     */
    @Operation(summary = "分页查询页面表", description = "分页查询页面表")
    @GetMapping("/pageSysPage")
    public Page<SysPageModel> pageSysPage(SysPageQuery query) {
        return sysPageService.pageSysPage(query);
    }
}